﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using UIWindows.Models.ParameterModels;

namespace UIWindows.GlobalStatics
{
    [Attributes.Singleton]
    public class ParameterStatic
    {
        public CollectionItemsParameterModel CollectionItemsParameter { get; set; }
        public DecideParameterModel DecideParameter { get; set; }
        public MESParameterModel MESParameter { get; set; }
        public RangeParameterModel RangeParameter { get; set; }
        public RunParameterModel RunParameter { get; set; }

       
        public ParameterStatic()
        {

            if (File.Exists(GlobalStatic.ParameterPath))
            {               
                try
                {
                    var model = JsonSerializer.Deserialize<Dictionary<string, object>>(File.ReadAllText(GlobalStatic.ParameterPath, System.Text.Encoding.UTF8));
                    RangeParameter = JsonSerializer.Deserialize<RangeParameterModel>(model["RangeParameter"].ToString());
                    RunParameter = JsonSerializer.Deserialize<RunParameterModel>(model["RunParameter"].ToString());
                    DecideParameter = JsonSerializer.Deserialize<DecideParameterModel>(model["DecideParameter"].ToString());
                    MESParameter = JsonSerializer.Deserialize<MESParameterModel>(model["MESParameter"].ToString());
                    CollectionItemsParameter = JsonSerializer.Deserialize<CollectionItemsParameterModel>(model["CollectionItemsParameter"].ToString());
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"注意==>>参数初始化异常：{ex}");
                }
            }
            else
            {
                RunParameter = new RunParameterModel();
                DecideParameter = new DecideParameterModel();
                MESParameter = new MESParameterModel();
                RangeParameter = new RangeParameterModel();
                CollectionItemsParameter = new CollectionItemsParameterModel();
            }
        }

        public void Save() => System.IO.File.WriteAllText(GlobalStatic.ParameterPath, JsonSerializer.Serialize(this, GlobalStatic.Options), Encoding.UTF8);
        
    }
}
